package com.example.sunmiv1.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.VipPlusAdapter;
import com.example.sunmiv1.adapter.VipRechargeAdapter;
import com.example.sunmiv1.entity.VipRechargeListBean;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.utils.zxing.Constant;
import com.example.sunmiv1.utils.zxing.activity.CaptureActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipRechargeActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.vipRecharge_online)
    LinearLayout vipRechargeOnline;
    @BindView(R.id.vipRecharge_money)
    LinearLayout vipRechargeMoney;
    @BindView(R.id.vipRecharge_gridView)
    GridView vipRechargeGridView;
    @BindView(R.id.vipRecharge_confirm)
    TextView vipRechargeConfirm;
    @BindView(R.id.vipRechargeMoney_off)
    ImageView vipRechargeMoneyOff;
    @BindView(R.id.vipRechargeMoney_recharge)
    EditText vipRechargeMoneyRecharge;
    @BindView(R.id.vipRechargeMoney_give)
    EditText vipRechargeMoneyGive;
    @BindView(R.id.vipRechargeMoney_confirm)
    TextView vipRechargeMoneyConfirm;
    @BindView(R.id.vipRechargeMoney_lin)
    LinearLayout vipRechargeMoneyLin;
    @BindView(R.id.vipRechargePass_off)
    ImageView vipRechargePassOff;
    @BindView(R.id.vipRechargePass_pass)
    EditText vipRechargePassPass;
    @BindView(R.id.vipRechargePass_login)
    TextView vipRechargePassLogin;
    @BindView(R.id.vipRechargePass_confirm)
    TextView vipRechargePassConfirm;
    @BindView(R.id.vipRechargePass_linear)
    LinearLayout vipRechargePassLinear;
    @BindView(R.id.vipRechargeLogin_off)
    ImageView vipRechargeLoginOff;
    @BindView(R.id.vipRechargeLogin_phone)
    EditText vipRechargeLoginPhone;
    @BindView(R.id.vipRechargeLogin_pass)
    EditText vipRechargeLoginPass;
    @BindView(R.id.vipRechargeLogin_cancel)
    TextView vipRechargeLoginCancel;
    @BindView(R.id.vipRechargeLogin_confirm)
    TextView vipRechargeLoginConfirm;
    @BindView(R.id.vipRechargeLogin_linear)
    LinearLayout vipRechargeLoginLinear;
    @BindView(R.id.vipRechargeMoney_code)
    EditText vipRechargeMoneyCode;
    @BindView(R.id.vipRechargeMoney_codeLin)
    LinearLayout vipRechargeMoneyCodeLin;
    @BindView(R.id.vipRecharge_plusTv)
    TextView vipRechargePlusTv;
    @BindView(R.id.vipRecharge_plus)
    GridView vipRechargePlus;
    @BindView(R.id.vipRechargeMoney_scan)
    ImageView vipRechargeMoneyScan;
    @BindView(R.id.tv_rechargeDialog_rechargeMoney)
    TextView tvRechargeDialogRechargeMoney;
    @BindView(R.id.tv_rechargeDialog_giveMoney)
    TextView tvRechargeDialogGiveMoney;
    @BindView(R.id.tv_rechargeDialog_type)
    TextView tvRechargeDialogType;
    @BindView(R.id.tv_rechargeDialog_activeTime)
    TextView tvRechargeDialogActiveTime;
    @BindView(R.id.linear_rechargeDialog_activeTime)
    LinearLayout linearRechargeDialogActiveTime;
    @BindView(R.id.tv_rechargeDialog_agingTime)
    TextView tvRechargeDialogAgingTime;
    @BindView(R.id.linear_rechargeDialog_agingTime)
    LinearLayout linearRechargeDialogAgingTime;
    @BindView(R.id.linear_rechargeCoupon)
    LinearLayout linearRechargeCoupon;

    private Context context;
    private ACache aCache;
    private VipRechargeAdapter vipRechargeAdapter;
    private VipPlusAdapter vipPlusAdapter;
    private LoadingDialog loadingDialog;
    private int payIndex = 9999;//普通充值優惠
    private int plusIndex = 9999;//成爲plus會員
    private int renewPlusIndex = 9999;//續費plus會員
    private int payType = 1;//1.在線支付  2.現金支付
    private SharedPreferences sp;
    private int user_type = 1;//充值時登陸的身份，1,是收銀員，2,是店長
    private VipRechargeListBean vipRechargeListBean;
    private List<VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean> rechargeList = new ArrayList<>();
    private List<VipRechargeListBean.RechargeActivityBean.PlusRechargeActivityBean> plusList = new ArrayList<>();
    private List<VipRechargeListBean.RechargeActivityBean.PlusRechargeActivityBean> renewPlusList = new ArrayList<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if ("1".equals(vipRechargeListBean.getCustom_recharge())) {
                        vipRechargeMoneyRecharge.setEnabled(true);
                    } else {
                        vipRechargeMoneyRecharge.setEnabled(false);
                    }
                    if ("2".equals(vipRechargeListBean.getCustom_give())) {
                        vipRechargeMoneyGive.setEnabled(true);
                    } else {
                        vipRechargeMoneyGive.setEnabled(false);
                    }
                    VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean bean = new VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean();
                    bean.setId("0");
                    rechargeList.add(bean);
                    for (VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean vipRechargeActivityBean : vipRechargeListBean.getRecharge_activity().getVip_recharge_activity()) {
                        rechargeList.add(vipRechargeActivityBean);
                    }
                    if (vipRechargeListBean.getVip_type() == 1) {
                        for (VipRechargeListBean.RechargeActivityBean.PlusRechargeActivityBean plusRechargeActivityBean : vipRechargeListBean.getRecharge_activity().getPlus_recharge_activity()) {
                            plusList.add(plusRechargeActivityBean);
                        }
                        if (plusList.size() > 0) {
                            vipRechargePlusTv.setText(R.string.vipRecharge1);
                            vipRechargePlus.setVisibility(View.VISIBLE);
                        } else {
                            vipRechargePlusTv.setVisibility(View.GONE);
                            vipRechargePlus.setVisibility(View.GONE);
                        }
                        setVipPlusAdapter();
                    } else if (vipRechargeListBean.getVip_type() == 2 || vipRechargeListBean.getVip_type() == 3) {
                        for (VipRechargeListBean.RechargeActivityBean.PlusRechargeActivityBean plusRechargeActivityBean : vipRechargeListBean.getRecharge_activity().getPlus_recharge_activity()) {
                            renewPlusList.add(plusRechargeActivityBean);
                        }
                        if (renewPlusList.size() > 0) {
                            vipRechargePlusTv.setText(R.string.vipRecharge2);
                            vipRechargePlus.setVisibility(View.VISIBLE);
                        } else {
                            vipRechargePlusTv.setVisibility(View.GONE);
                            vipRechargePlus.setVisibility(View.GONE);
                        }
                        setRenewPlusAdapter();
                    }
                    setVipRechargeAdapter();
                    break;
                case 2:
                    VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean bean1 = new VipRechargeListBean.RechargeActivityBean.VipRechargeActivityBean();
                    bean1.setId("0");
                    rechargeList.add(bean1);
                    setVipRechargeAdapter();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_vip_recharge);
        ButterKnife.bind(this);
        sp = getSharedPreferences("context", Activity.MODE_PRIVATE);//創建sp對象
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        initListener();
        getRechargeList();
    }

    private void initListener() {
        vipRechargeGridView.setOnItemClickListener((parent, view, position, id) -> {
                    payIndex = position;
                    plusIndex = 9999;
                    renewPlusIndex = 9999;
                    vipRechargeAdapter.isSelect(payIndex);
                    if(vipPlusAdapter!=null) {
                        vipPlusAdapter.isSelect(renewPlusIndex);
                    }

                    //分期到賬
                    if ("0".equals(rechargeList.get(payIndex).getId())) {
                        linearRechargeCoupon.setVisibility(View.GONE);
                    } else {
                        linearRechargeCoupon.setVisibility(View.VISIBLE);
                    }
                    tvRechargeDialogRechargeMoney.setText(rechargeList.get(payIndex).getRecharge_money());
                    tvRechargeDialogGiveMoney.setText(rechargeList.get(payIndex).getGive_money());
                    tvRechargeDialogType.setText(rechargeList.get(payIndex).getAging_repay_name());
                    if ("1".equals(rechargeList.get(payIndex).getAging_repay())) {
                        linearRechargeDialogActiveTime.setVisibility(View.VISIBLE);
                        linearRechargeDialogAgingTime.setVisibility(View.VISIBLE);
                        tvRechargeDialogActiveTime.setText(rechargeList.get(payIndex).getActive_aging_time());
                        tvRechargeDialogAgingTime.setText(rechargeList.get(payIndex).getAging_time());
                    } else {
                        linearRechargeDialogActiveTime.setVisibility(View.GONE);
                        linearRechargeDialogAgingTime.setVisibility(View.GONE);
                    }
                }
        );
        vipRechargePlus.setOnItemClickListener((parent, view, position, id) -> {
            if (vipRechargeListBean.getVip_type() == 1) {
                payIndex = 9999;
                renewPlusIndex = 9999;
                plusIndex = position;
                vipPlusAdapter.isSelect(plusIndex);
            } else {
                payIndex = 9999;
                renewPlusIndex = position;
                plusIndex = 9999;
                vipPlusAdapter.isSelect(renewPlusIndex);
            }
            vipRechargeAdapter.isSelect(payIndex);


            if (vipRechargeListBean.getVip_type() == 1) {
                if ("0".equals(plusList.get(plusIndex).getId())) {
                    linearRechargeCoupon.setVisibility(View.GONE);
                } else {
                    linearRechargeCoupon.setVisibility(View.VISIBLE);
                }
                //分期到賬
                tvRechargeDialogRechargeMoney.setText(plusList.get(plusIndex).getRecharge_money());
                tvRechargeDialogGiveMoney.setText(plusList.get(plusIndex).getGive_money());
                tvRechargeDialogType.setText(plusList.get(plusIndex).getAging_repay_name());
                if ("1".equals(plusList.get(plusIndex).getAging_repay())) {
                    linearRechargeDialogActiveTime.setVisibility(View.VISIBLE);
                    linearRechargeDialogAgingTime.setVisibility(View.VISIBLE);
                    tvRechargeDialogActiveTime.setText(plusList.get(plusIndex).getActive_aging_time());
                    tvRechargeDialogAgingTime.setText(plusList.get(plusIndex).getAging_time());
                } else {
                    linearRechargeDialogActiveTime.setVisibility(View.GONE);
                    linearRechargeDialogAgingTime.setVisibility(View.GONE);
                }
            } else {
                if ("0".equals(renewPlusList.get(renewPlusIndex).getId())) {
                    linearRechargeCoupon.setVisibility(View.GONE);
                } else {
                    linearRechargeCoupon.setVisibility(View.VISIBLE);
                }
                //分期到賬
                tvRechargeDialogRechargeMoney.setText(renewPlusList.get(renewPlusIndex).getRecharge_money());
                tvRechargeDialogGiveMoney.setText(renewPlusList.get(renewPlusIndex).getGive_money());
                tvRechargeDialogType.setText(renewPlusList.get(renewPlusIndex).getAging_repay_name());
                if ("1".equals(renewPlusList.get(renewPlusIndex).getAging_repay())) {
                    linearRechargeDialogActiveTime.setVisibility(View.VISIBLE);
                    linearRechargeDialogAgingTime.setVisibility(View.VISIBLE);
                    tvRechargeDialogActiveTime.setText(renewPlusList.get(renewPlusIndex).getActive_aging_time());
                    tvRechargeDialogAgingTime.setText(renewPlusList.get(renewPlusIndex).getAging_time());
                } else {
                    linearRechargeDialogActiveTime.setVisibility(View.GONE);
                    linearRechargeDialogAgingTime.setVisibility(View.GONE);
                }
            }
        });
        vipRechargeMoneyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() == 18) {

                }
            }
        });
        vipRechargeMoneyRecharge.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(".")) {
                    if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 2) {
                        charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 3);
                        vipRechargeMoneyRecharge.setText(charSequence);
                        vipRechargeMoneyRecharge.setSelection(charSequence.length());
                    }
                }
                if (charSequence.toString().trim().substring(0).equals(".")) {
                    charSequence = "0" + charSequence;
                    vipRechargeMoneyRecharge.setText(charSequence);
                    vipRechargeMoneyRecharge.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!charSequence.toString().substring(1, 2).equals(".")) {
                        vipRechargeMoneyRecharge.setText(charSequence.subSequence(1, 2));
                        vipRechargeMoneyRecharge.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        vipRechargeMoneyGive.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains(".")) {
                    if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 2) {
                        charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 3);
                        vipRechargeMoneyGive.setText(charSequence);
                        vipRechargeMoneyGive.setSelection(charSequence.length());
                    }
                }
                if (charSequence.toString().trim().substring(0).equals(".")) {
                    charSequence = "0" + charSequence;
                    vipRechargeMoneyGive.setText(charSequence);
                    vipRechargeMoneyGive.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!charSequence.toString().substring(1, 2).equals(".")) {
                        vipRechargeMoneyGive.setText(charSequence.subSequence(1, 2));
                        vipRechargeMoneyGive.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void setVipRechargeAdapter() {
        vipRechargeAdapter = new VipRechargeAdapter(context, rechargeList);
        vipRechargeGridView.setAdapter(vipRechargeAdapter);
    }

    private void setVipPlusAdapter() {
        vipPlusAdapter = new VipPlusAdapter(context, plusList);
        vipRechargePlus.setAdapter(vipPlusAdapter);
    }

    private void setRenewPlusAdapter() {
        vipPlusAdapter = new VipPlusAdapter(context, renewPlusList);
        vipRechargePlus.setAdapter(vipPlusAdapter);
    }

    @OnClick({R.id.title_back, R.id.vipRecharge_online, R.id.vipRecharge_money, R.id.vipRecharge_confirm,
            R.id.vipRechargeMoney_off, R.id.vipRechargeMoney_confirm, R.id.vipRechargeMoney_lin, R.id.vipRechargePass_off,
            R.id.vipRechargePass_login, R.id.vipRechargePass_confirm, R.id.vipRechargePass_linear, R.id.vipRechargeLogin_off,
            R.id.vipRechargeLogin_cancel, R.id.vipRechargeLogin_confirm, R.id.vipRechargeLogin_linear, R.id.vipRechargeMoney_scan,
            R.id.linear_rechargeCoupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_rechargeCoupon:
                linearRechargeCoupon.setVisibility(View.GONE);
                break;
            case R.id.vipRechargeMoney_scan:
                startQrCode();
                break;
            case R.id.title_back:
                finish();
                break;
            case R.id.vipRecharge_online:
                payType = 1;
                vipRechargeOnline.setBackgroundResource(R.drawable.red_border);
                vipRechargeMoney.setBackgroundResource(R.drawable.search_border);
                vipRechargeMoneyCode.setText("");
                vipRechargeMoneyCodeLin.setVisibility(View.VISIBLE);
                break;
            case R.id.vipRecharge_money:
                payType = 2;
                vipRechargeOnline.setBackgroundResource(R.drawable.search_border);
                vipRechargeMoney.setBackgroundResource(R.drawable.red_border);
                vipRechargeMoneyCode.setText("");
                vipRechargeMoneyCodeLin.setVisibility(View.GONE);
                break;
            case R.id.vipRecharge_confirm:
                if (payIndex != 9999) {
                    vipRechargeMoneyRecharge.setText(rechargeList.get(payIndex).getRecharge_money());
                    vipRechargeMoneyGive.setText(rechargeList.get(payIndex).getGive_money());
                } else if (plusIndex != 9999) {
                    vipRechargeMoneyRecharge.setText(plusList.get(plusIndex).getRecharge_money());
                    vipRechargeMoneyGive.setText(plusList.get(plusIndex).getGive_money());
                } else if (renewPlusIndex != 9999) {
                    vipRechargeMoneyRecharge.setText(renewPlusList.get(renewPlusIndex).getRecharge_money());
                    vipRechargeMoneyGive.setText(renewPlusList.get(renewPlusIndex).getGive_money());
                } else {
                    Toast.makeText(context, "請選擇充值優惠", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (payIndex != 0) {
                    vipRechargeMoneyGive.setEnabled(false);
                } else {
                    vipRechargeMoneyGive.setEnabled(true);
                }
                vipRechargeMoneyLin.setVisibility(View.VISIBLE);
                break;

            case R.id.vipRechargeMoney_off:
                vipRechargeMoneyRecharge.setText("");
                vipRechargeMoneyGive.setText("");
                vipRechargeMoneyCode.setText("");
                vipRechargeMoneyLin.setVisibility(View.GONE);
                break;
            case R.id.vipRechargeMoney_confirm:
                if (TextUtils.isEmpty(vipRechargeMoneyRecharge.getText().toString().trim()) ||
                        Double.parseDouble(vipRechargeMoneyRecharge.getText().toString().trim()) <= 0) {
                    Toast.makeText(context, "請輸入正確的充值金額", Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (!TextUtils.isEmpty(vipRechargeMoneyGive.getText().toString().trim()) &&
//                        Double.parseDouble(vipRechargeMoneyGive.getText().toString().trim()) >
//                                Double.parseDouble(vipRechargeMoneyRecharge.getText().toString().trim())) {
//                    Toast.makeText(context, "贈送金額不能大於充值金額", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (payIndex == 9999 && plusIndex == 9999 && renewPlusIndex == 9999) {
                    Toast.makeText(context, "請選擇充值方式", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (payType == 1 && vipRechargeMoneyCode.getText().toString().trim().length() != 18) {
                    Toast.makeText(context, "請輸入付款碼", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (payType == 1) {
                    recharge();
                } else {
                    user_type = 1;
                    vipRechargePassLinear.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.vipRechargeMoney_lin:
                break;
            case R.id.vipRechargePass_off:
                vipRechargePassPass.setText("");
                vipRechargePassLinear.setVisibility(View.GONE);
                break;
            case R.id.vipRechargePass_login:
                user_type = 2;
                vipRechargePassPass.setText("");
                vipRechargePassLinear.setVisibility(View.GONE);
                vipRechargeLoginLinear.setVisibility(View.VISIBLE);
                break;
            case R.id.vipRechargePass_confirm:
                recharge();
                break;
            case R.id.vipRechargePass_linear:
                break;
            case R.id.vipRechargeLogin_off:
                user_type = 1;
                vipRechargeLoginPhone.setText("");
                vipRechargeLoginPass.setText("");
                vipRechargeLoginLinear.setVisibility(View.GONE);
                break;
            case R.id.vipRechargeLogin_cancel:
                user_type = 1;
                vipRechargeLoginPhone.setText("");
                vipRechargeLoginPass.setText("");
                vipRechargeLoginLinear.setVisibility(View.GONE);
                break;
            case R.id.vipRechargeLogin_confirm:
                recharge();
                break;
            case R.id.vipRechargeLogin_linear:
                break;
        }
    }

    // 開始掃碼
    private void startQrCode() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 申請權限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
            return;
        }
        // 二維碼掃碼
        Intent intent = new Intent(context, CaptureActivity.class);
        startActivityForResult(intent, Constant.REQ_QR_CODE);
    }

    /*權限回調*/
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case Constant.REQ_PERM_CAMERA:
                // 攝像頭權限申請
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲得授權
                    startQrCode();
                } else {
                    // 被禁止授權
                    Toast.makeText(context, "請至權限中心打開本應用的相機訪問權限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    /*頁面回調*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        //Log.e(TAG, "onActivityResult: " + data);
        switch (requestCode) {
            case Constant.REQ_QR_CODE:
                if (data != null) {
                    Bundle bundle = data.getExtras();
                    vipRechargeMoneyCode.setText(bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN));
                }
                break;
        }

    }

    /*獲取充值優惠*/
    private void getRechargeList() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("vip_id", getIntent().getStringExtra("vid"));
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(context, Common.GET_RECHARGE_COUPON_LIST, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    vipRechargeListBean = GsonUtils.JsonToBean(text, VipRechargeListBean.class);
                    Message msg = new Message();
                    if (vipRechargeListBean.getStatus() == 1) {
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /*會員充值*/
    private void recharge() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("vip_id", getIntent().getStringExtra("vid"));
            jsonObject.put("vip_type", vipRechargeListBean.getVip_type());
            jsonObject.put("user_type", user_type);
            if (payType == 2) {
                jsonObject.put("pay_type", 1);
            } else if (payType == 1) {
                jsonObject.put("pay_type", 2);
                jsonObject.put("pay_code", vipRechargeMoneyCode.getText().toString());
            }

            if (user_type == 1) {
                jsonObject.put("recharge_code", vipRechargePassPass.getText().toString());
            } else {
                jsonObject.put("manager_number", vipRechargeLoginPhone.getText().toString());
                jsonObject.put("manager_pass", vipRechargeLoginPass.getText().toString());
            }

            jsonObject.put("recharge_money", vipRechargeMoneyRecharge.getText().toString());
            if (payIndex != 9999) {
                jsonObject.put("recharge_id", rechargeList.get(payIndex).getId());
            } else if (plusIndex != 9999) {
                jsonObject.put("recharge_id", plusList.get(plusIndex).getId());
            } else if (renewPlusIndex != 9999) {
                jsonObject.put("recharge_id", renewPlusList.get(renewPlusIndex).getId());
            } else {
                jsonObject.put("recharge_id", 0);
            }

            if (!TextUtils.isEmpty(vipRechargeMoneyGive.getText().toString().trim()) &&
                    Double.parseDouble(vipRechargeMoneyGive.getText().toString().trim()) > 0) {
                jsonObject.put("give_money", vipRechargeMoneyGive.getText().toString().trim());
            } else {
                jsonObject.put("give_money", 0);
            }
            String store_id = aCache.getAsString("storeid");
            String merchant_id = aCache.getAsString("merchantid");
            String cashier_id = aCache.getAsString("uid");
            jsonObject.put("store_id", TextUtils.isEmpty(store_id) ? sp.getString("store_id", "") : store_id);
            jsonObject.put("merchant_id", TextUtils.isEmpty(merchant_id) ? sp.getString("merchant_id", "") : merchant_id);
            jsonObject.put("storeid", TextUtils.isEmpty(store_id) ? sp.getString("store_id", "") : store_id);
            jsonObject.put("merchantid", TextUtils.isEmpty(merchant_id) ? sp.getString("merchant_id", "") : merchant_id);
            if ("2".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("operator_id"));
            } else if ("1".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }else{
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }
            if("3".equals(aCache.getAsString("userType"))) {
                jsonObject.put("cashier_id", TextUtils.isEmpty(cashier_id) ? sp.getString("cashier_id", "") : cashier_id);
            }
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(context, Common.RECHARGE_MONEY, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("msg"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            vipRechargeMoneyLin.setVisibility(View.GONE);
                            vipRechargePassLinear.setVisibility(View.GONE);
                            vipRechargeLoginLinear.setVisibility(View.GONE);
                            vipRechargePassPass.setText("");
                            vipRechargeLoginPhone.setText("");
                            vipRechargeLoginPass.setText("");
                            user_type = 1;
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                protected void onFailure(ResponseException e) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
